<?php
namespace app\shop\controller;

use \think\Controller;
use \think\Db;
class Driver extends Base
{
    /**
     * 配送员首页：index
     * @$page 分页
     * @return array 返回数组
     */
    public function index()
    {
		extract(input());
        $auth = $this->checkAllDriver($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $where = array();
        $where['order_driver_id'] = $user_id;//公共条件
        //1、今日配送和所有配送
        $order = array();
        $order['today'] = Db::table('up_order')
            ->where($where)
            ->whereTime('order_paytime', 'today')
            ->count();
        $order['month'] = Db::table('up_order')
            ->where($where)
            ->whereTime('order_paytime', 'm')
            ->count();
        $order['all'] = Db::table('up_order')
            ->where($where)
            ->count();
        //2、今日配送订单
        $field = 'order_id,order_number,order_paytime,order_driver_status';
        $field .= ',order_address_name,order_address,order_address_phone';
        $data = Db::table('up_order')
            ->where($where)
            ->whereTime('order_paytime', 'today')
            ->field($field)
            ->order('order_driver_status ASC')
            ->page($page,10)
            ->select();
        return json_encode(['status'=>200,'order'=>$order,'msg'=>$data]);
    }
    /**
     * 历史配送:historyOrder
     * @ page 分页
     * @ date 日期
     * @return array 返回数组
     */
    public function historyOrder()
    {
		extract(input());
        $auth = $this->checkAllDriver($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $where = array();
        $where['order_driver_id'] = $user_id;//公共条件
        $where['order_paydate'] = $date;//公共条件
        $field = 'order_id,order_number,order_paytime,order_driver_status';
        $field .= ',order_address_name,order_address,order_address_phone';
        $data = Db::table('up_order')
            ->where($where)
            ->field($field)
            ->order('order_driver_status ASC')
            ->page($page,10)
            ->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 订单详情：orderInfo
     * order_id
     * @return string
     */
    public function orderInfo()
    {
        extract(input());
        $auth = $this->checkAllDriver($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $where = array();
        $where['order_id'] = $order_id;
        $order = Db::table('up_order')->alias('o')
            ->join('up_shops s','o.order_shopid=s.s_id','LEFT')
            ->where($where)
            ->field('o.*,s.s_id,s.s_name,s_phone,s_thumb')
            ->find();
        $goods = Db::table('up_order_info')
            ->where('oi_order_id',$order_id)
            ->select();
        return json_encode(['status'=>200,'order'=>$order,'goods'=>$goods]);
    }

    /**
     * 接单或已送达：orderReciveAndOver
     * order_id，type（2是去配送，3是已送达）
     * @return string
     */
    public function orderReciveAndOver()
    {
        extract(input());
        $auth = $this->checkAllDriver($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        if($type != 2 && $type != 3){
            return json_encode(['status'=>0,'msg'=>'传参错误！']);
        }
        $update = array();
        $update['order_driver_status'] = $type;
        if($type == 3){//如果是2(配送员选择没)
            $update['order_send_status'] = 3;
        }
        $update['order_last_time'] = date('Y-m-d H:i:s');
        $where = array();
        $where['order_id'] = $order_id;
        $res = Db::table('up_order')->where($where)->update($update);
        if($res){
            return json_encode(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'操作失败']);
        }

    }
}